import api from "../../../api/api.js"
import consts from "../../../utils/consts.js"
import courseUtil from "../../../utils/course.js"

Page({
  /**
   * 页面的初始数据
   */
  data: {
    weeks: [],
    times: [],
    durations: [],
    title: '',
    week: 0,
    stime: "",
    duration: 60,
    location: {}
  },
  // 获取课程标题
  bindCourseTitlelBlur: function (e) {
    this.setData({
      title: e.detail.value
    })
  },

  // 获取上课周期
  bindCourseWeekChange: function (e) {
    var sel = Number(e.detail.value)
    this.setData({
      week: sel
    })
  },

  // 获取课程上课时间
  bindCourseSTimeChange: function (e) {
    this.setData({
      stime: e.detail.value
    })
  },

  // 获取课程上课时长
  bindCourseDurationChange: function (e) {
    var sel = Number(e.detail.value)
    this.setData({
      duration: sel
    })
  },

  // 选择瑜伽课位置
  bindCourseLocationTap: function (e) {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        if (res.name || res.address) {
          that.setData({
            location: res
          })
        } else {
          wx.showToast({
            title: '请选择详细地址！',
            icon: "none",
            duration: 1000
          })
        }
      }
    })
  },

  /**
   * 添加课程
   */
  bindSubmitTap: function () {
    if (!this.data.title) {
      wx.showToast({
        title: '必填项不能为空',
        icon: 'none',
        duration: 1000
      })
    } else {
      if (!this.data.courseId) {// 添加
        api.saveCourse({
          method: 'get',
          data: {
            openid: getApp().globalData.openid,
            title: this.data.title,
            week: this.data.week,
            stime: this.data.stime,
            duration: this.data.duration,
            l_name: this.data.location.name,
            l_address: this.data.location.address,
            l_latitude: this.data.location.latitude,
            l_longitude: this.data.location.longitude
          },
          success: function (resp) {
            if (resp.code == 0) {
              courseUtil.setCourses(courseUtil.parseCourses(resp.data))
              // 返回
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.showToast({
                title: resp.message,
                icon: 'none',
                duration: 1000
              })
            }
          }
        })

      } else {// 修改
        api.updateCourse({
          method: 'get',
          data: {
            id: this.data.courseId,
            openid: getApp().globalData.openid,
            title: this.data.title,
            week: this.data.week,
            stime: this.data.stime,
            duration: this.data.duration,
            l_name: this.data.location.name,
            l_address: this.data.location.address,
            l_latitude: this.data.location.latitude,
            l_longitude: this.data.location.longitude
          },
          success: function (resp) {
            if (resp.code == 0) {
              courseUtil.setCourses(courseUtil.parseCourses(resp.data))
              // 返回
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.showToast({
                title: resp.message,
                icon: 'none',
                duration: 1000
              })
            }
          }
        })
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 1000
        })
        wx.navigateBack({
          delta: 1
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id

    this.setData({
      weeks: consts.weeks,
      times: consts.times.slice(getApp().globalData.userConf.startTime, getApp().globalData.userConf.endTime),
      durations: consts.durations
    })

    var that = this
    if (id) {// 修改课程
      wx.setNavigationBarTitle({
        title: '修改课程',
      })
      api.getCourse({
        method: 'get',
        data: { id: id },
        success: function (resp) {
          if (resp.code == 0) {
            var course = courseUtil.parseCourse(resp.data)
            that.setData({
              courseId: id,
              title: course.title,
              week: course.week,
              stime: course.stimeStr,
              duration: course.duration,
              location: {
                name: course.l_name,
                address: course.l_address,
                latitude: course.l_latitude,
                longitude: course.l_longitude
              }
            })
          }
        }
      })

    } else {// 添加课程
      wx.setNavigationBarTitle({
        title: '添加课程',
      })
      this.setData({
        stime: this.data.times[0]
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})